﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;

using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Text;

namespace WP7SampleProject12
{
	public partial class Page1 : PhoneApplicationPage
	{
        private StringBuilder Url = new StringBuilder();
		public Page1()
		{
			InitializeComponent();
            if (Location.updated)
            {
                locationCity.Text = Location.city;
                locationState.Text = Location.state;
            }
            else
            {
                locationCity.Text = "";
                locationState.Text = "";
            }
            String name_temp = GlobalVariables.movieName;
            String[] name_temps = name_temp.Split(' ');
            char[] char_temp;
            string temp0;
            String temp_list = "";
            foreach (String temp in name_temps)
            {
                temp0 = temp.ToLower();
                char_temp = temp0.ToCharArray();
                char_temp[0] = char_temp[0].ToString().ToUpper().ToCharArray()[0];
                temp_list = temp_list + " " + new string(char_temp);
            }
            movieName.Text = temp_list.Trim();
            
            myprogressbar.IsIndeterminate = true;
            loading.Visibility = System.Windows.Visibility.Collapsed;


            GlobalVariables.stage = 0;
            movieImage.Source = new BitmapImage(new Uri("http://www.glamsham.com/movies/images/ishaqzaade.jpg"));
            Url.Append("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=jkroks");
            
            movieImage.Visibility = System.Windows.Visibility.Collapsed;
            Likes.Visibility = System.Windows.Visibility.Collapsed;
            Dislikes.Visibility = System.Windows.Visibility.Collapsed;
            Text_Likes.Visibility = System.Windows.Visibility.Collapsed;
            Text_Dislikes.Visibility = System.Windows.Visibility.Collapsed;
            lookuptweet.IsEnabled = false;
            yahooReview.IsEnabled = false;

            if (GlobalVariables.positive.Equals("NA"))
                updateData("http://tweetmyflickmobile.appspot.com/tweetmyflickmobile?flick=" + GlobalVariables.movieName);
            else
            {
                Text_Likes.Text = GlobalVariables.positive;
                Text_Dislikes.Text = GlobalVariables.negative;
                myprogressbar.IsIndeterminate = false;

                movieImage.Visibility = System.Windows.Visibility.Visible;
                Likes.Visibility = System.Windows.Visibility.Visible;
                Dislikes.Visibility = System.Windows.Visibility.Visible;
                Text_Likes.Visibility = System.Windows.Visibility.Visible;
                Text_Dislikes.Visibility = System.Windows.Visibility.Visible;
                lookuptweet.IsEnabled = true;
                yahooReview.IsEnabled = true;
                myprogressbar.Visibility = System.Windows.Visibility.Collapsed;
            }
            
		}

        private void updateData(String Url)
        {

                WebClient wc = new WebClient();
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadDataCompleted);
                wc.DownloadStringAsync(new Uri(Url.ToString()));
            
        }

        private void twitter_DownloadDataCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            String result = e.Result;
            String[] results = result.Split(':');

            String positive = results[1].Split(':')[0].Split('<')[0];
            String negative = results[2].Split(':')[0].Split('<')[0];
            
            Text_Likes.Text = positive;
            Text_Dislikes.Text = negative;

            GlobalVariables.positive = positive;
            GlobalVariables.negative = negative;

            myprogressbar.IsIndeterminate = false;

            movieImage.Visibility = System.Windows.Visibility.Visible;
            Likes.Visibility = System.Windows.Visibility.Visible;
            Dislikes.Visibility = System.Windows.Visibility.Visible;
            Text_Likes.Visibility = System.Windows.Visibility.Visible;
            Text_Dislikes.Visibility = System.Windows.Visibility.Visible;
            lookuptweet.IsEnabled = true;
            yahooReview.IsEnabled = true;
            myprogressbar.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
        }

        private void review_click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/reviews.xaml", UriKind.Relative));

            foreach (string obj in listBox1.Items)
            {
                
            }
        }


        

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Yahoo_Reviews.Text = "";

            myprogressbar.IsIndeterminate = true;
            loading.Visibility = System.Windows.Visibility.Visible;
            movieImage.Visibility = System.Windows.Visibility.Collapsed;
            WebClient twitter = new WebClient();
            twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
            twitter.DownloadStringAsync(new Uri(Url.ToString()));
            GlobalVariables.stage = 1;
            Likes.Visibility = System.Windows.Visibility.Collapsed;
            Text_Likes.Visibility = System.Windows.Visibility.Collapsed;
            Dislikes.Visibility = System.Windows.Visibility.Collapsed;
            Text_Dislikes.Visibility = System.Windows.Visibility.Collapsed;
            
        }

        void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            XElement xmlTweets = XElement.Parse(e.Result);

            listBox1.ItemsSource = from tweets in xmlTweets.Descendants("status")
                                   select new TwitterItem
                                   {
                                       ImageSource = tweets.Element("user").Element("profile_image_url").Value,
                                       Message = tweets.Element("text").Value,
                                       UserName = tweets.Element("user").Element("screen_name").Value
                                   };

            myprogressbar.IsIndeterminate = false;
            loading.Visibility = System.Windows.Visibility.Collapsed;
        
        }
        
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {


            
            movieImage.Visibility = System.Windows.Visibility.Collapsed;

            TurnstileTransition rotatetransition = new TurnstileTransition();
            ITransition transition = rotatetransition.GetTransition(Yahoo_Reviews);
            transition.Completed += delegate { transition.Stop(); };
            transition.Begin();

            

            listBox1.ItemsSource = null;
            Yahoo_Reviews.Text = "Cast: Akshay Kumar, Asin Thottumkal, John Abraham, Jacqueline Fernandez, Riteish Deshmukh, Zarine Khan, Shreyas Talpade, Shazahn Padamsee, Mithun Chakraborty, Rishi Kapoor, Randhir Kapoor, Boman Irani \n \t Directed by Sajid Khan \n \t Rating: * \n Comedy fueled by confusion yields very little. And in Bollywood, this leads to people repeating their punch lines, screaming while spitting with expressions more animated than the next Pixar production. Here we have a house full of just this. The success factors are the usual: foreign locations, a minimum of two top stars (John-Akshay) and a maximum of two average stars to make the top stars feel more significant (Shreyas-Riteish). Then there has to be unaccountable opulence (since the audience wants to escape from their middle-class drudgery), dhishooming at the ratio of 30:1, naach-gaana by the united blues of Thailand and so on.";
            Likes.Visibility = System.Windows.Visibility.Collapsed;
            Text_Likes.Visibility = System.Windows.Visibility.Collapsed;
            Dislikes.Visibility = System.Windows.Visibility.Collapsed;
            Text_Dislikes.Visibility = System.Windows.Visibility.Collapsed;
            GlobalVariables.stage = 2;

        }



        private void Contact_Us(object sender, RoutedEventArgs e)
        {
            MessageBoxButton b = new MessageBoxButton();
            MessageBox.Show("Email us at:", "Contact us", b);

        }

        private void About_Us(object sender, RoutedEventArgs e)
        {
            MessageBoxButton b = new MessageBoxButton();
            MessageBox.Show("TweetMyFlick is a blablabalabla", "About TweetMyFlick", b);

        }


        

        private void Back_call(object sender, RoutedEventArgs e)
        {
            if (GlobalVariables.stage == 0)
            {
                SlideTransition rotatetransition = new SlideTransition();
                //rotatetransition.Mode = RotateTransitionMode.In90Clockwise;
                //rotatetransition.Mode = SlideTransitionMode.SlideRightFadeIn;
                rotatetransition.Mode = SlideTransitionMode.SlideRightFadeIn;
                PhoneApplicationPage phoneApplicationPage =
                (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
                ITransition transition = rotatetransition.GetTransition(phoneApplicationPage);
                transition.Completed += delegate { transition.Stop(); };
                transition.Begin();
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
            if (GlobalVariables.stage == 1)
            {

                listBox1.ItemsSource = null;
                GlobalVariables.stage = 0;
                movieImage.Visibility = System.Windows.Visibility.Visible;
                Likes.Visibility = System.Windows.Visibility.Visible;
                Text_Likes.Visibility = System.Windows.Visibility.Visible;
                Dislikes.Visibility = System.Windows.Visibility.Visible;
                Text_Dislikes.Visibility = System.Windows.Visibility.Visible;
                Yahoo_Reviews.Text = "";
            }
            if (GlobalVariables.stage == 2)
            {
                movieImage.Visibility = System.Windows.Visibility.Collapsed;
                WebClient twitter = new WebClient();
                twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
                twitter.DownloadStringAsync(new Uri(Url.ToString()));
                GlobalVariables.stage = 0;
                Yahoo_Reviews.Text = "";
            }
        }

        private void movieImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
           
        }

        private void Dislikes_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void Back_Click(object sender, EventArgs e)
        {

        }

        private void movieTime(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/movieShowtime.xaml", UriKind.Relative));

        }
	}
    public class TwitterItem
    {
        public string UserName { get; set; }
        public string Message { get; set; }
        public string ImageSource { get; set; }

    }
}